import axios from '@/libs/api.request'

// 表单ID与列表请求对应
const formPage = {
  '8': 'job',// 应聘
}
/**
 * 获取表单信息
 *
 * @param {Number} form_id 表单Id
 */
export const getForm = (form_id) => {
  return axios.request({
    url: `forms/${form_id}`,
    method: 'get'
  })
}

/**
 * 获取表格显示列
 *
 * @param {Number} form_id 表单Id
 */
export const getColumns = (form_id) => {
  return axios.request({
    url: `forms/${form_id}/view`,
    method: 'get'
  })
}

/**
 * 修改自定义显示列
 *
 * @param {Array} list 显示列数组
 */
export const updateColumn = (form_id, list) => {
  return axios.request({
    url: `forms/${form_id}/view`,
    method: 'put',
    data: {
      list
    }
  })
}


/**
 * 获取自定义检索条件
 *
 * @param {Number} form_id 表单Id
 */
export const getFilters = (form_id) => {
  return axios.request({
    url: `forms/${form_id}/filter`,
    method: 'get'
  })
}

/**
 * 编辑自定义检索条件
 *
 * @param {Number} filter_id 筛选ID
 * @param {Object} filter 查询条件 {title:'xxx',order:'',list:[]}
 */
export const updateFilter = (filter_id, filter) => {
  return axios.request({
    url: `forms/filter/${filter_id}`,
    method: 'put',
    data: {
      ...filter
    }
  })
}

/**
 * 删除自定义筛选
 *
 * @param {Number} filter_id  筛选id
 */
export const deleteFilter = (filter_id, filter) => {
  return axios.request({
    url: `forms/filter/${filter_id}`,
    method: 'delete'
  })
}

/**
 * 新增自定义筛选
 *
 * @param {Number} form_id  表单id
 * @param {Object} filter 查询条件 {title:'xxx',order:'',list:[]}
 */
export const addFilter = (form_id, filter) => {
  return axios.request({
    url: `forms/${form_id}/filter`,
    method: 'post',
    data: {
      ...filter
    }
  })
}

/**
 * 获取表格数据
 *
 * @param {Object} filter 查询条件
 * {
 * "page_info":{
 * page:1,
 * per_page:10
 * },
 * conditions:[
 * [
 * "field_name":"name",
 * "type":1,//查询值type 查询条件: 1 等于 2 不等于 3 包含 4 不包含 5 起始字符 6 大于 7 大于等于 8 小于 9 小于等于 10
 * 今天(value值为空) 11 昨天(value值为空) 12 本月(value值为空) 13 上月(value值为空) 14 今年(value值为空) 15
 * 去年(value值为空) 16 过去多少天内 17 过去多少天前 18 在未来多少天内 19 在未来多少天前 20 在指定多少天后  21在指定多少天前 22为空 23 包括 24
 * 不为空 25 区间查询(值类型为以逗号隔开的两个时间戳拼成的字符串)
 * "value":"用户"
 * ],[...]
 * ],
 * sort:[{"field_name":"name", "direction":"asc"}, ...] // direction可供选项[asc|desc]
 * keywords: {"field_names":["keywords1"],"value": ""},
 * }
 */
export const getTableData = (filter) => {
  const pageUrl = formPage[filter.form_id]

  return axios.request({
    url: pageUrl + '/index',
    method: 'post',
    data: {
      ...filter
    }
  })
}

/**
 *
 *
 * @param {Number} form_id 表单Id
 * @param {Number} list 字段列表
 */
export const updateFormFields = (form_id, list) => {
  return axios.request({
    url: `forms/${form_id}/fields`,
    method: 'put',
    data: {
      list
    }
  })
}

/**
 * 获取树形表格数据
 *
 * @param {Number} form_id 表单Id
 */
export const getTreeTableData = (form_id) => {
  return axios.request({
    url: 'get_tree_table_data',
    method: 'get',
    params: {
      form_id
    }
  })
}

/**
 * 获取表单列表
 *
 */
export const getFormList = () => {
  return axios.request({
    url: 'forms/page',
    method: 'post',
    data: {}
  })
}

/**
 * 获取表单字段列表
 *
 * @param {Number} form_id 表单Id
 */
export const getFormFields = (form_id) => {
  return axios.request({
    url: `forms/${form_id}/fields`,
    method: 'get',
    params: {}
  })
}

/**
 * 修改表单单个字段配置
 *
 * @param {Number} form_id 表单Id
 * @param {Object} obj 字段配置
 */
export const updateSingleFormField = (form_id, obj) => {
  return axios.request({
    url: `forms/fields/${form_id}`,
    method: 'post',
    data: {
      obj
    }
  })
}

/**
 * 新建表单数据
 *
 * @param {String} form_id 表单ID
 * @param {Object} data 数据
 */
export const createFormData = (form_id, data) => {
  const url = formPage[form_id]

  return axios.request({
    url: `${url}/store`,
    method: 'post',
    data
  })
}

/**
 * 更新表单数据
 *
 * @param {String} url 请求路由地址
 * @param {Object} params 表单数据
 */
export const updateFormData = (form_id, data) => {
  const url = formPage[form_id]

  return axios.request({
    url: `${url}/update/${data.id}`,
    method: 'put',
    data
  })
}

/**
 * 批量删除数据
 *
 * @param {String} form_id 表单Id
 * @param {Object} ids Ids
 */
export const deletesFormData = (form_id, ids) => {
  const url = formPage[form_id]

  return axios.request({
    url: `${url}/destroy`,
    method: 'delete',
    data: {
      ids
    }
  })
}

/**
 * 删除数据
 *
 * @param {String} form_id 表单ID
 * @param {Object} params 表单数据
 */
export const deleteFormData = (form_id, id) => {
  const url = formPage[form_id]

  return axios.request({
    url: `${url}/delete/${id}`,
    method: 'delete'
  })
}

/**
 * 获取详情
 *
 * @param {String} form_id 表单Id
 * @param {Object} params 表单数据
 */
export const getFormDetail = (form_id, id) => {
  const url = formPage[form_id]

  return axios.request({
    url: `${url}/show/${id}`,
    method: 'get'
  })
}

/**
 * 退款审核
 *
 * @param {String} id 订单id
 * @param {Object} status 审核状态
 */
export const approveData = (id, status) => {
  return axios.request({
    url: `order/return/${id}/type/${status}`,
    method: 'get'
  })
}

/**
 * 添加备注
 *
 * @param {String} id id
 * @param {String} form_id 表单Id
 * @param {Object} remark 备注
 */
export const dataRemark = (form_id, id, remark) => {
  const url = formPage[form_id]

  return axios.request({
    url: `${url}/remark/${id}`,
    method: 'put',
    data: {
      remark
    }
  })
}

/**
 * 设置会员日默认图片
 *
 * @param {String} id 图片id
 */
export const setVipDayPic = (id) => {
  return axios.request({
    url: `memberday/default/${id}`,
    method: 'put',
    data: {
      is_default: 2
    }
  })
}

/**
 * 会员日图片列表
 *
 */
export const getVipPic = () => {
  return axios.request({
    url: `memberday/index`,
    method: 'get'
  })
}

/**
 * 删除会员日图片
 *
 * @param {String} id 图片ID
 */
export const deleteVipPic = (id) => {
  return axios.request({
    url: `memberday/destroy/${id}`,
    method: 'delete'
  })
}

/**
 * 首页今日订单总计
 *
 */
export const getHomeOrderData = () => {
  return axios.request({
    url: `statistic/today`,
    method: 'get'
  })
}

/**
 * 首页订单走势
 *
 */
export const getHomeOrderStatistic = (data) => {
  let url = `statistic/order`;
  if (data.stime) {
    url = `statistic/order/${data.stime}/${data.etime}`;
  }
  return axios.request({
    url,
    method: 'get'
  })
}

/**
 * 产品上架，下架
 *
 */
export const productOnline = (id, type) => {
  return axios.request({
    url: `goods/online/${id}/${type}`,
    method: 'put'
  })
}
